/*
 * JFrGamesOptions.java
 *
 * Created on 24 septembre 2007, 16:18
 */

package gotha;

import java.rmi.*;
import java.awt.*;
import java.awt.event.*;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author  Administrateur
 */
public class JFrGamesOptions extends javax.swing.JFrame{

    private static final long REFRESH_DELAY = 2000;
    private long lastComponentsUpdateTime = 0;
    
    private TournamentInterface tournament;
    /** Creates new form JFrGamesOptions */
    public JFrGamesOptions() {
        initComponents();
        setupRefreshTimer();
    }

    private void setupRefreshTimer(){
        ActionListener taskPerformer = new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                try {
                    if (tournament.getLastTournamentModificationTime() > lastComponentsUpdateTime) {
                        updateAllViews();
                    }
                } catch (RemoteException ex) {
                    Logger.getLogger(JFrGamesResults.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        new javax.swing.Timer((int) REFRESH_DELAY, taskPerformer).start();
    }

    public JFrGamesOptions(TournamentInterface tournament) throws RemoteException{
        this.tournament = tournament;
        
        initComponents();
        customInitComponents();
        setupRefreshTimer();
    }        
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlGam = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txfTime = new javax.swing.JTextField();
        txfSize = new javax.swing.JTextField();
        txfKomi = new javax.swing.JTextField();
        ckbByoYomi = new javax.swing.JCheckBox();
        btnQuit = new javax.swing.JButton();
        btnHelp = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Games Parameters");
        setResizable(false);
        getContentPane().setLayout(null);

        pnlGam.setLayout(null);

        jLabel1.setText("Thinking time (min)");
        pnlGam.add(jLabel1);
        jLabel1.setBounds(10, 30, 160, 14);

        jLabel2.setText("Go-Ban size");
        pnlGam.add(jLabel2);
        jLabel2.setBounds(10, 80, 160, 14);

        jLabel3.setText("Komi");
        pnlGam.add(jLabel3);
        jLabel3.setBounds(10, 130, 160, 14);

        txfTime.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txfTimeFocusLost(evt);
            }
        });
        pnlGam.add(txfTime);
        txfTime.setBounds(170, 30, 50, 20);

        txfSize.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txfSizeFocusLost(evt);
            }
        });
        pnlGam.add(txfSize);
        txfSize.setBounds(170, 80, 50, 20);

        txfKomi.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txfKomiFocusLost(evt);
            }
        });
        pnlGam.add(txfKomi);
        txfKomi.setBounds(170, 130, 50, 20);

        ckbByoYomi.setText("Byo Yomi");
        ckbByoYomi.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        ckbByoYomi.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        ckbByoYomi.setMargin(new java.awt.Insets(0, 0, 0, 0));
        ckbByoYomi.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ckbByoYomiFocusLost(evt);
            }
        });
        pnlGam.add(ckbByoYomi);
        ckbByoYomi.setBounds(270, 30, 100, 15);

        btnQuit.setText("Quit");
        btnQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitActionPerformed(evt);
            }
        });
        pnlGam.add(btnQuit);
        btnQuit.setBounds(145, 180, 200, 30);

        btnHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gotha/gothalogo16.jpg"))); // NOI18N
        btnHelp.setText("help");
        btnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHelpActionPerformed(evt);
            }
        });
        pnlGam.add(btnHelp);
        btnHelp.setBounds(10, 180, 110, 30);

        getContentPane().add(pnlGam);
        pnlGam.setBounds(0, 0, 400, 260);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ckbByoYomiFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ckbByoYomiFocusLost
        try {
            TournamentParameterSet tps = tournament.getTournamentParameterSet();
            GeneralParameterSet gps = tps.getGeneralParameterSet();
            gps.setBByoYomi(this.ckbByoYomi.isSelected());
            tournament.setTournamentParameterSet(tps);
            this.tournamentChanged();
        }catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_ckbByoYomiFocusLost

    private void txfKomiFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txfKomiFocusLost
        try {
            TournamentParameterSet tps = tournament.getTournamentParameterSet();
            GeneralParameterSet gps = tps.getGeneralParameterSet();
            gps.setStrKomi(txfKomi.getText());
            tournament.setTournamentParameterSet(tps);
            this.tournamentChanged();
            } catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_txfKomiFocusLost

    private void txfSizeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txfSizeFocusLost
        try {
            TournamentParameterSet tps = tournament.getTournamentParameterSet();
            GeneralParameterSet gps = tps.getGeneralParameterSet();
            gps.setStrSize(txfSize.getText());
            tournament.setTournamentParameterSet(tps);
            this.tournamentChanged();
            } catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_txfSizeFocusLost

    private void txfTimeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txfTimeFocusLost
        try {
            TournamentParameterSet tps = tournament.getTournamentParameterSet();
            GeneralParameterSet gps = tps.getGeneralParameterSet();
            gps.setStrTime(txfTime.getText());
            tournament.setTournamentParameterSet(tps);
            this.tournamentChanged();
            } catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_txfTimeFocusLost

    private void btnQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitActionPerformed
        dispose();
    }//GEN-LAST:event_btnQuitActionPerformed

    private void btnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHelpActionPerformed
        Gotha.displayGothaHelp("Games Options");
}//GEN-LAST:event_btnHelpActionPerformed

    private void customInitComponents() throws RemoteException{
        int w = JFrGotha.MEDIUM_FRAME_WIDTH;
        int h = JFrGotha.MEDIUM_FRAME_HEIGHT;
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((dim.width - w)/2, (dim.height -h)/2, w, h);

        setIconImage(Gotha.getIconImage());

        setIconImage(Gotha.getIconImage());
        this.updatePnlGam();
    }
    
    public void tournamentChanged(){
        try {
            if (!tournament.isOpen()){
                dispose();
                return;
            }
            tournament.setLastTournamentModificationTime(tournament.getCurrentTournamentTime());
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }

        updateAllViews();
    }

    private void updateAllViews(){
        this.pnlGam.setVisible(true);
        try {
            this.lastComponentsUpdateTime = tournament.getCurrentTournamentTime();
            setTitle("Games Parameters. " + tournament.getTournamentParameterSet().getGeneralParameterSet().getShortName());
            updatePnlGam();
        } catch (RemoteException ex) {
            Logger.getLogger(JFrGamesOptions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void updatePnlGam() throws RemoteException{
        GeneralParameterSet gps = null;
        gps = tournament.getTournamentParameterSet().getGeneralParameterSet();
        this.txfTime.setText(gps.getStrTime());
        this.ckbByoYomi.setSelected(gps.isBByoYomi());
        this.txfSize.setText(gps.getStrSize());
        this.txfKomi.setText(gps.getStrKomi());
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHelp;
    private javax.swing.JButton btnQuit;
    private javax.swing.JCheckBox ckbByoYomi;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel pnlGam;
    private javax.swing.JTextField txfKomi;
    private javax.swing.JTextField txfSize;
    private javax.swing.JTextField txfTime;
    // End of variables declaration//GEN-END:variables
    
}
